
export {};


let deal = (value,format)=>{
    if(format == ''){
        return value;
    }

    format = format.toLowerCase().trim();//不区分大小
    let match_func = format.match(/^[a-z|A-Z]+/gi);//匹配到 format 中的 函数名
    let match_num = format.match(/\d+$/gi);   //匹配到 format 中的参数
    let func: string = '';
    let num: number;
    let res: number | string = '';

    if (match_func) func = match_func[0];
    if (match_num) num = parseInt(match_num[0]);

    if (typeof value == 'number') {
        switch (func) {
            case 'int': res = int(value); break;
            case 'fix': res = fix(value, num); break;
            case 'kmbt': res = KMBT(value); break;
            case 'per': res = per(value, num); break;
            case 'pad': res = pad(value, num); break;
            case 'sep': res = sep(value); break;
            case 'mmss': res = mmss(value); break;
            case 'hhmmss': res = hhmmss(value); break;

            default:
                break;
        }

    } else {
        switch (func) {
            case 'limit': res = limit(value, num); break;

            default:
                break;
        }
        // res = value;
    }

    return res;
}

/** [value:per2] 将取值0~1 变成 1~100,可以指定修饰的小数位数 */
function per(value: number, fd: number) {
    return (value * 100).toFixed(fd);
}

/** [value:int] 将取值变成整数 */
function int(value: number) {
    return Math.round(value);
}

/** [value:pad2]  补位参数2: 1 显示为 01   参数 3: 1 显示为 001 */
function pad(value, fd: number) {
    var len = value.toString().length;
    while(len < fd){
        value = "0" + value;
        len++;
    }
    return value;
}

/** [value:fix2]数值转换为小数*/
function fix(value: number, fd: number) {
    return value.toFixed(fd)
}

/** [value:limit3]字符串长度限制 */
function limit(value: string, count: number) {
    return value.substring(0, count);
}

//将数字按分号显示
function sep(value: number){
    let num = Math.round(value).toString();
    return num.replace(new RegExp('(\\d)(?=(\\d{3})+$)', 'ig'), "$1,");
}
/** 将数字缩短显示为KMBT单位 大写,目前只支持英文 */
function KMBT(value: number) {
    //10^4=万, 10^8=亿,10^12=兆,10^16=京，
    let counts = [1000, 1e6, 1e9, 1e12,1e15,1e18,1e21,1e24,1e27];
    let units = ['','k', 'm', 'b', 't','aa','bb','cc','dd','ee'];
    return compressUnit(value, counts, units, 2);
}

/** 时:分:秒 */
function hhmmss(value: number){
    return parseTimer(value,true);
}
/** 分:秒 */
function mmss(value: number){
    return parseTimer(value,false);
}
/** 时间格式转换 */
function parseTimer(timer =0,isFullTimer = true){
    let t = Math.floor(timer);
    let hours = Math.floor( t/3600);
    let mins =  Math.floor( (t%3600)/60);
    let secs =  t%60;
    let h = ''+hours;
    let m = ''+mins;
    let s = ''+secs;
    if(secs<10)s = '0'+secs;
    if(hours<10)h = '0'+hours;
    
    //full timer 按小时算,无论有没有小时
    if(isFullTimer){
        if(mins<10) m = '0' + mins;
        return  h+':'+m+':'+s;
    }else{
        // m = ''+ (mins +hours*60);
        // if(mins<10) m = '0' + mins;
        // return m+':'+s;
        if(mins<10) m = '0' + mins;
        if(hours > 0){
            return  h+':'+m+':'+s;
        }
        return m+':'+s;
    }
}

//压缩任意单位的数字，后缀加上单位文字
function compressUnit(value, valueArr: number[], unitArr: string[], fixNum: number = 2): string {
    let counts = valueArr;
    let units = unitArr;
    let res: string;
    let index;
    for (index = 0; index < counts.length; index++) {
        const e = counts[index];
        if (value < e) {
            if (index > 0) {
                res = (value / counts[index - 1]).toFixed(fixNum);
            } else {
                res = value.toFixed(0);
            }
            break;
        }

    }
    return res + units[index];
}

export let StringFunction = {
    deal
}
